/*
 * @(#)WheatBeerListActivity.java $version 2013. 5. 23.
 *
 * Copyright 2007 NHN Corp. All rights Reserved. 
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package com.design.oop;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Set;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Toast;

import com.design.oop.bean.WheatBeerDegreeComparator;
import com.design.oop.bean.WheatBeerKind;
import com.design.oop.bean.WheatBeerKind.SortType;
import com.design.oop.bean.WheatBeerRateComparator;
import com.design.oop.control.CustomScrollView;
import com.design.oop.control.MemoListAdapter;
import com.design.oop.control.MemoListAdapter.MemoListAdapterListener;
import com.design.oop.control.MemoListView;
import com.design.oop.util.BookmarkPreference;
import com.design.oop.util.CommonUtil;
import com.design.oop.util.NToast;

/**
 * @author kelly
 */
public class WheatBeerListActivity extends Activity {
	private static Toast toast;
	CustomScrollView scrollView;
	MemoListView itemListView;
	MemoListAdapter itemListAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.beer_list);

		Bundle bundle = getIntent().getExtras();
		ArrayList<WheatBeerKind> beerList = new ArrayList<WheatBeerKind>();

		if (bundle != null && bundle.containsKey("bookmark")) {
			if (bundle.getBoolean("bookmark", false)) {
				Set<Integer> bookmarkSet = BookmarkPreference.getBookmarkList(this);
				if (CommonUtil.isEmptySet(bookmarkSet)) {
					NToast.showToast(toast, WheatBeerListActivity.this, "즐겨찾기 한 맥주가 없습니다.", true);
					finish();
					return;
				}

				for (int bookmark : bookmarkSet) {
					beerList.add(WheatBeerKind.BEER_LIST.get(bookmark));
				}
			} else {
				beerList.addAll(WheatBeerKind.BEER_LIST);
			}
		} else {
			beerList.addAll(WheatBeerKind.BEER_LIST);
		}

		SortType sortType = SortType.ENAME;
		if (bundle != null && bundle.containsKey("sortType")) {
			int sortTypeValue = bundle.getInt("sortType");
			sortType = SortType.of(sortTypeValue);
			if (SortType.DEGREE == sortType) {
				Collections.sort(beerList, new WheatBeerDegreeComparator());
			} else if (SortType.RATE == sortType) {
				Collections.sort(beerList, new WheatBeerRateComparator());
			}
		}

		scrollView = (CustomScrollView)findViewById(R.id.scroll);
		itemListView = (MemoListView)findViewById(R.id.list_adapter);
		itemListAdapter = new MemoListAdapter(this);
		itemListAdapter.setItemListAdapterListener(adapterListener);
		itemListAdapter.setItemListView(itemListView);

		scrollView.setOnScrollChangedListener(onScrollChangedListener);

		itemListView.setContainingScrollView(scrollView);
		itemListView.setAdapter(itemListAdapter);
		itemListAdapter.setDataSource(WheatBeerKind.BEER_LIST);

		Handler handler = new Handler();
		handler.post(new Runnable() {
			@Override
			public void run() {
				refreshViews();
			}
		});
	}

	private void refreshViews() {
		if (itemListView != null) {
			itemListView.requestLayout();
		}
		if (scrollView != null) {
			scrollView.invalidate();
			scrollView.requestLayout();
		}
		if (itemListAdapter != null) {
			itemListAdapter.finishLoading();
		}
	}

	private CustomScrollView.OnScrollChangedListener onScrollChangedListener = new CustomScrollView.OnScrollChangedListener() {
		@Override
		public void onScrollChanged(CustomScrollView scrollView, int left, int top, int oldLeft, int oldTop) {
			scrollView.requestLayout();

			if (itemListView != null) {
				itemListView.onScrollChanged();
			}
		}
	};

	private MemoListAdapterListener adapterListener = new MemoListAdapterListener() {
		@Override
		public void loadMoreData() {
		}

		@Override
		public void onClickItem(int itemId) {
			Intent intent = new Intent(WheatBeerListActivity.this, WheatBeerDetailActivity_.class);
			intent.putExtra("id", itemId);
			startActivity(intent);
		}

		@Override
		public void onImportantCheckedItem(long contentId, boolean isChecked) {
		}
	};
}
